﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WeatherStation;

namespace WeatherStationView
{
    /// <summary>
    /// Interaction logic for ForecastDisplay.xaml
    /// </summary>
    public partial class ForecastDisplay : UserControl, IObserver
    {
        public ForecastDisplay()
        {
            InitializeComponent();
        }

        #region ForecastVariables

        private float currentPressure = 29.92f;
        private float lastPressure;

        #endregion

        #region IObserver Members

        public void Update(float temperature, float humidity, float pressure)
        {
            lastPressure = currentPressure;
            currentPressure = pressure;

            ForecastType forecast;

            if (currentPressure > lastPressure)
            {
                forecast = ForecastType.Improving;
            }
            else if (currentPressure == lastPressure)
            {
                forecast = ForecastType.NotChanging;
            }
            else
            {
                forecast = ForecastType.Deteriorating;
            }

            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new setValuesDelegate(setValues), forecast);
        }

        #endregion

        #region WeatherData suscription related

        private Boolean suscribedToWD = false;

        private ISubject wd;

        public ISubject WeatherDataSource
        {
            get
            {
                return wd;
            }
            set
            {
                wd = value;
            }
        }

        #endregion

        #region GUI logic

        private enum ForecastType {Improving, NotChanging, Deteriorating};

        private delegate void setValuesDelegate(ForecastType forecast);

        private void setValues(ForecastType forecast)
        {

            BitmapImage myBitmapImage = new BitmapImage();

            myBitmapImage.BeginInit();            

            if(forecast == ForecastType.Improving)
            {
                myBitmapImage.UriSource = new Uri("pack://application:,,/sunny.png");
                lblForecast.Text = "Improving weather on the way!";
            }
            else if (forecast == ForecastType.NotChanging)
            {
                myBitmapImage.UriSource = new Uri("pack://application:,,/cloudy.png");
                lblForecast.Text = "More of the same";
            }
            else
            {
                myBitmapImage.UriSource = new Uri("pack://application:,,/rainy.png");
                lblForecast.Text = "Watch out for cooler, rainy weather";
            }

            myBitmapImage.DecodePixelWidth = 200;
            myBitmapImage.EndInit();
            imgForecast.Source = myBitmapImage;
        }

        private void btnSuscribeUnsuscribe_Click(object sender, RoutedEventArgs e)
        {
            if(wd == null) return;

            if (suscribedToWD)
            {
                wd.RemoveObserver(this);
                btnSuscribeUnsuscribe.Content = "Suscribe to Weather Data Source";
            }
            else
            {
                wd.RegisterObserver(this);
                btnSuscribeUnsuscribe.Content = "Unsuscribe from Weather Data Source";
            }

            suscribedToWD = !suscribedToWD;
        }

        #endregion

    }
}
